<template>
  <div class="mod-skate__fathlete">
    <el-form :inline="true" :model="dataForm" @keyup.enter="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.username" placeholder="请输入姓名" style="width: 120px" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.phone" placeholder="请输入电话" style="width: 120px" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.idCard" placeholder="请输入身份证号" clearable></el-input>
      </el-form-item>
      <!--状态，下拉框-->
      <el-form-item>
        <el-select v-model="dataForm.flag" placeholder="请选择状态" style="width: 120px" clearable>
          <el-option key="0" label="未缴费" value="0"></el-option>
          <el-option key="1" label="已缴费" value="1"></el-option>
          <el-option key="2" label="过期" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="俱乐部" prop="clubId">
        <el-select v-model="dataForm.clubId" placeholder="俱乐部">
          <el-option v-for="item in clubList" :key="item.id" :label="item.clubName" :value="parseInt(item.id)"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="exportHandle()">{{ $t("export") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="hasPermission('skate:fathlete:save')" type="primary" @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="hasPermission('skate:fathlete:renzheng')" type="primary" @click="batchRZhHandle()">批量认证</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="importExcel">导入</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="downExcel">下载模板</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="hasPermission('skate:fathlete:delete')" type="danger" @click="deleteHandle()">{{ $t("deleteBatch") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="id" label="id" header-align="center" align="center"></el-table-column>
      <el-table-column prop="number" label="编号" header-align="center" align="center" width="120"></el-table-column>
      <el-table-column prop="username" label="姓名" header-align="center" align="center" width="120"></el-table-column>
      <el-table-column prop="phone" label="电话" header-align="center" align="center" width="130"></el-table-column>
      <el-table-column prop="idCard" label="身份证号" header-align="center" align="center" width="170"></el-table-column>
      <el-table-column prop="gender" label="性别" header-align="center" align="center">
        <template v-slot="scope">
          <span v-if="scope.row.gender === 1">男</span>
          <span v-else-if="scope.row.gender === 0">女</span>
        </template>
      </el-table-column>
      <el-table-column prop="clubName" label="俱乐部" header-align="center" align="center" width="230"></el-table-column>
      <el-table-column prop="clubName" label="状态" header-align="center" align="center">
        <template v-slot="scope">
          <span v-if="scope.row.flag === '0'">未认证</span>
          <span v-else-if="scope.row.flag === '1'">已认证</span>
          <span v-else-if="scope.row.flag === '2'">过期</span>
        </template>
      </el-table-column>
      <el-table-column prop="expirationDate" label="过期时间" header-align="center" align="center" width="160"></el-table-column>
      <el-table-column prop="img" label="图片" header-align="center" align="center">
        <template v-slot="scope">
          <img v-if="scope.row.img" :src="scope.row.img" class="avatar" style="width: 50px; height: 50px" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="200">
        <template v-slot="scope">
          <el-button v-if="hasPermission('skate:fathlete:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t("update") }}</el-button>
          <el-button v-if="hasPermission('skate:fathlete:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t("delete") }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle"> </el-pagination>
    <import v-if="importVisible" ref="import" @refreshDataList="getDataList"></import>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script lang="ts">
import useView from "@/hooks/useView";
import { defineComponent, reactive, toRefs } from "vue";
import baseService from "@/service/baseService";
import AddOrUpdate from "./fathlete-add-or-update.vue";
import Import from "./fathlete-import.vue";
export default defineComponent({
  components: {
    AddOrUpdate,
    Import
  },
  setup() {
    const state = reactive({
      getDataListURL: "/skate/fathlete/page",
      getDataListIsPage: true,
      exportURL: "/skate/fathlete/export",
      deleteURL: "/skate/fathlete",
      deleteIsBatch: true,
      dataForm: {
        username: "",
        phone: "",
        idCard: "",
        flag: "",
        clubId: null
      },
      importVisible: false,
      clubList: [],
      selectedClub: ""
    });
    return { ...useView(state), ...toRefs(state) };
  },
  watch: {},
  created() {
    this.queryClubList();
  },
  methods: {
    importExcel() {
      this.importVisible = true;
      this.$nextTick(() => {
        this.$refs.import.init();
      });
    },
    // 批量认证
    batchRZhHandle() {
      // 循环取dataListSelections的id
      let ids: string[] = [];
      for (let i = 0; i < this.dataListSelections!.length; i++) {
        // let id = this.dataListSelections![i].id;
        let flag = this.dataListSelections![i].flag;
        if (flag !== "0") {
          return this.$message.error("请选择待认证的数据");
        }
        ids.push(this.dataListSelections![i].id);
      }
      if (ids.length === 0) {
        return this.$message.error("请选择要认证的数据");
      }
      this.$confirm("确认认证通过？", this.$t("prompt.title"), {
        confirmButtonText: this.$t("confirm"),
        cancelButtonText: this.$t("cancel"),
        type: "warning"
      }).then(() => {
        baseService.post("/skate/fathlete/batchRenZheng", ids).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          } else {
            this.query();
          }
        });
      });
    },
    // 筛选条件
    queryClubList() {
      baseService.get("/skate/fclub/list").then((response) => {
        this.clubList = response.data;
        if (this.clubList.length == 1) {
          this.dataForm.clubId = parseInt(this.clubList[0].id + "");
        }
      });
    },
    downExcel() {
      let a = document.createElement("a");
      a.href = "./excel/运动员模板.xlsx";
      a.style.display = "none";
      document.body.appendChild(a);
      a.click();
      a.remove();
    }
  }
});
</script>
